﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Medianamik.Core.DAL.SQL
{
    public class GroupByClause : ISqlQueryPart 
    {
        private List<Column> _columns;
        public GroupByClause()
        {
            _columns = new List<Column>();
        }

        public GroupByClause AddColumn(Column pColumn)
        {
            return AddColumns(new[] {pColumn});
        }
        public GroupByClause AddColumns(IEnumerable<Column> pColumns )
        {
            foreach (var column in pColumns)
            {
                _columns.Add(column);
            }
            return this;
        }
        #region ISqlQueryPart Members

        public StringBuilder ToSQL()
        {
            if(_columns.Count == 0)
            {
                return new StringBuilder();
            }
            var groupBy = new StringBuilder(" GROUP BY ");

            groupBy.Append(_columns.First().NameWithAlias);
            foreach (var c in Enumerable.Skip(_columns,1))
            {
                groupBy.Append(", " + c.NameWithAlias);
            }
            return groupBy;
        }

        #endregion
    }
}
